package org.ptkparty.client.view.games;

import java.io.IOException;
import java.util.List;

import org.ptkparty.client.activity.PrepareGamesActivity;
import org.ptkparty.client.model.PlaceProxy;
import org.ptkparty.client.model.UserProxy;
import org.ptkparty.client.ui.UserSelectionCellListView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ParagraphElement;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;

public class PrepareGamesView extends Composite {

	private static PrepareGameViewUiBinder uiBinder = GWT.create(PrepareGameViewUiBinder.class);

	interface PrepareGameViewUiBinder extends UiBinder<Widget, PrepareGamesView> {
	}

	private PrepareGamesActivity activity;
	private ListDataProvider<UserProxy> provider;

	@UiField
	ParagraphElement errorMsg;

	@UiField(provided = true)
	UserSelectionCellListView playerList;

	@UiField(provided = true)
	ValueListBox<PlaceProxy> boulodromeList;

	@UiField
	Button generateBtn;

	@UiHandler("generateBtn")
	public void onGenerateBtnClicked(ClickEvent event) {
		if (boulodromeList.getValue() == null) {
			showError("Veuillez renseigner le boulodrome");
			return;
		}
		if (playerList.getValue().isEmpty() || playerList.getValue().size() < 4) {
			showError("Veuillez renseigner au moins 4 joueurs");
			return;
		}
		activity.generate(boulodromeList.getValue().getId(), //
				playerList.getValue());
	}

	public PrepareGamesView() {
		playerList = new UserSelectionCellListView(new MultiSelectionModel<UserProxy>());

		provider = new ListDataProvider<UserProxy>();
		provider.addDataDisplay(playerList);
		playerList.setProvider(provider);

		boulodromeList = new ValueListBox<PlaceProxy>(new Renderer<PlaceProxy>() {

			@Override
			public String render(PlaceProxy object) {
				return object == null ? "" : object.getName();
			}

			@Override
			public void render(PlaceProxy object, Appendable appendable) throws IOException {
				appendable.append(render(object));
			}
		});

		initWidget(uiBinder.createAndBindUi(this));

		hideError();
	}

	public void hideError() {
		this.errorMsg.getStyle().setDisplay(Display.NONE);
	}

	public void showError(String errorMsg) {
		this.errorMsg.setInnerText(errorMsg);
		this.errorMsg.getStyle().setDisplay(Display.BLOCK);
	}

	public void setPresenter(PrepareGamesActivity addGameActivity) {
		activity = addGameActivity;
	}

	public void loadUser(List<UserProxy> response) {
		provider.setList(response);
	}

	public void loadPlaces(List<PlaceProxy> response) {
		if (response != null && !response.isEmpty()) {
			boulodromeList.setAcceptableValues(response);
			boulodromeList.setValue(response.get(0));
		}

	}

}
